<?php
namespace app\admin\controller;
use think\view;
use think\Db;
use think\Session;
use Qiniu\Auth as Auth;
class User extends CommonController
{
    //用户管理
    public function index()
    {
        if(!empty($res['p'])){
            $p = $res['p'];
            $this->assign('p',$p);
        }
        $count = Db::name('user')->count(); //计算记录数
        $limitRows = 30; // 设置每页记录数
        $p = new AjaxPage($count, $limitRows,"news"); //第三个参数是你需要调用换页的ajax函数名

        $limit_value = $p->firstRow . "," . $p->listRows;
        $list = Db::name('user')->order('uptime desc')->limit($limit_value)->select();
        $page = $p->show(); // 产生分页信息，AJAX的连接在此处生成
        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

    //新增用户
    public  function  user_add(){
        if($_POST){
            $_POST['uptime'] = date('Y-m-d H:i:s',time());
            $_POST['token'] = time();
            $_POST['password'] = md5( $_POST['password']);
            $_POST['state'] = 1;
            $add = Db::name('user')->insert($_POST);
            if($add){
                $this->success('添加成功', url('User/index'));
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }
   //编辑用户信息
    public function user_edit()
    {
        if($_GET){
            $map['id']  = $_GET['id'];
            $list = Db::name('user')->where($map)->find();
            $this->assign('list',$list);
        }
        if($_POST){
            $map['id']  = $_POST['id'];
            $_POST['uptime'] = date('Y-m-d H:i:s',time());
            $list = Db::name('user')->update($_POST);
            if($list){
                $this->success('修改成功', url('User/index'));

            }else{
                $this->error('修改失败');
            }
        }

        return $this->fetch();
    }


    //删除用户信息
    public  function user_del(){
        if($_POST){
            $map['id'] = $_POST['id'];
            $res = Db::name('user')->where($map)->delete();
            if($res){
                return show(1,'删除成功');
            }else{
                return show(0,'删除失败');
            }
        }

    }

    /*
    * 禁用
    */
    public function yes(){
        $map['id'] = $_POST['id'];
        $_POST['state'] = 2;
        $res = Db::name('user')->where($map)->update($_POST);;
        if($res){
            return show(1,'禁用成功！');
        }else{
            return show(0,'禁用失败！');
        }
    }
    /*
    * 解禁
    */
    public function no(){
        $map['id'] = $_POST['id'];
        $_POST['state'] = 1;
        $res = Db::name('user')->where($map)->update($_POST);;
        if($res){
            return show(1,'解禁成功！');
        }else{
            return show(0,'解禁失败！');
        }
    }



}
